package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallGoodsSpecification;
import com.ctsi.ssdc.domain.LitemallGoodsSpecificationExample;
import com.ctsi.ssdc.service.LitemallGoodsSpecificationService;
import com.ctsi.ssdc.repository.LitemallGoodsSpecificationRepository;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallGoodsSpecification.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallGoodsSpecificationServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallGoodsSpecificationRepository, LitemallGoodsSpecification, Integer, LitemallGoodsSpecificationExample> 
	implements LitemallGoodsSpecificationService {

    @Autowired
    LitemallGoodsSpecificationRepository litemallGoodsSpecificationRepository;


    /**
    * GET  /litemallGoodsSpecifications : get the litemallGoodsSpecifications firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallGoodsSpecification> findFirstStringColumn(String specification,Pageable pageable){
        String str = specification;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallGoodsSpecificationExample litemallGoodsSpecificationExample = new LitemallGoodsSpecificationExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallGoodsSpecificationExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallGoodsSpecificationExample.createCriteria();
        } else{
            litemallGoodsSpecificationExample.createCriteria().andSpecificationLike("%" + str +"%");
        }
        List<LitemallGoodsSpecification>  data = litemallGoodsSpecificationRepository.selectByExample(litemallGoodsSpecificationExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallGoodsSpecificationRepository.countByExample(litemallGoodsSpecificationExample);
        }
        return new PageResult<LitemallGoodsSpecification>(data,count,count);
    }

    @Override
    public List<LitemallGoodsSpecification> queryByGid(Integer id) {
        LitemallGoodsSpecificationExample example = new LitemallGoodsSpecificationExample();
        example.or().andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        return litemallGoodsSpecificationRepository.selectByExample(example);
    }

    @Override
    public Object getSpecificationVoList(Integer id) {
        List<LitemallGoodsSpecification> goodsSpecificationList = queryByGid(id);

        Map<String, VO> map = new HashMap<>();
        List<VO> specificationVoList = new ArrayList<>();

        for (LitemallGoodsSpecification goodsSpecification : goodsSpecificationList) {
            String specification = goodsSpecification.getSpecification();
            VO goodsSpecificationVo = map.get(specification);
            if (goodsSpecificationVo == null) {
                goodsSpecificationVo = new VO();
                goodsSpecificationVo.setName(specification);
                List<LitemallGoodsSpecification> valueList = new ArrayList<>();
                valueList.add(goodsSpecification);
                goodsSpecificationVo.setValueList(valueList);
                map.put(specification, goodsSpecificationVo);
                specificationVoList.add(goodsSpecificationVo);
            } else {
                List<LitemallGoodsSpecification> valueList = goodsSpecificationVo.getValueList();
                valueList.add(goodsSpecification);
            }
        }

        return specificationVoList;
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }


    private class VO {
        private String name;
        private List<LitemallGoodsSpecification> valueList;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<LitemallGoodsSpecification> getValueList() {
            return valueList;
        }

        public void setValueList(List<LitemallGoodsSpecification> valueList) {
            this.valueList = valueList;
        }
    }
}
